package UILayer;

import java.awt.BorderLayout;
import ModelLayer.CurrentAccount;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;




import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.WindowConstants;
import javax.swing.border.EmptyBorder;

import ModelLayer.CustomerCollection;
import ModelLayer.LeaseCollection;
import ModelLayer.MachineCollection;
import ModelLayer.OrderCollection;
import ModelLayer.StaffMemberCollection;
import ModelLayer.Stock;
import javax.swing.JLabel;

public class MainMenuUITry extends JFrame
{

	private JPanel contentPane;
	

	/**
	 * Launch the application.
	 */
	/*public static void main(String[] args)
	{
		EventQueue.invokeLater(new Runnable()
		{
			public void run()
			{
				try
				{

				    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
					UIManager.setLookAndFeel(UIManager
							.getSystemLookAndFeelClassName());
					MainMenuUITry frame = new MainMenuUITry();
					frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
				    frame.setSize(screenSize);
				    frame.setVisible(true);    // FIRST visible = true
				    frame.setResizable(true); // THEN  resizable = false
					frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
				}
				catch (Exception e)
				{
					e.printStackTrace();
				}
			}
		});
	}*/

	/**
	 * Create the frame.
	 */
	public MainMenuUITry()
	{
		setBounds(30,30, 1300, 700);
		setBackground(Color.WHITE);
		MachineCollection.initialize();
		LeaseCollection.initialize();
		Stock.initialize();
		OrderCollection.initialize();
		CustomerCollection.initialize();
		StaffMemberCollection.initialize();

		JMenuBar menuBar = new JMenuBar();
		menuBar.setBorderPainted(false);
		menuBar.setFont(new Font("Segoe UI", Font.PLAIN, 12));
		menuBar.setForeground(Color.WHITE);
		setJMenuBar(menuBar);
		this.addWindowListener(new WindowAdapter() 
		{
			@Override
			public void windowClosing(WindowEvent e) 
			{
				
				Stock.save_data();
				CustomerCollection.save_all_data();
				OrderCollection.save_data();
				OrderCollection.save_discounts();
				StaffMemberCollection.save_data();
				MachineCollection.save_data();
				System.exit(0);

			}
		});
		
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);

		final JDesktopPane desktopPane = new JDesktopPane();
		contentPane.add(desktopPane, BorderLayout.CENTER);

		desktopPane.setBackground(new Color(255, 153, 0));
		

		JMenu customers = new JMenu("Customers");
		menuBar.add(customers);

		JMenuItem mntmNewMenuItem = new JMenuItem("Manage Customers");
		mntmNewMenuItem.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				Customers frame = new Customers();
				desktopPane.add(frame);
				frame.setVisible(true);
			}
		});
		customers.add(mntmNewMenuItem);

		JMenuItem mntmManageGroups = new JMenuItem("Manage Groups");
		mntmManageGroups.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				GroupsUI frame = new GroupsUI();
				desktopPane.add(frame);
				frame.setVisible(true);
			}
		});
		customers.add(mntmManageGroups);

		JButton btnNewButton_1 = new JButton("Products");
		btnNewButton_1.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				ProductUI frame = new ProductUI();
				desktopPane.add(frame);
				frame.setVisible(true);
			}
		});
		menuBar.add(btnNewButton_1);

		JButton btnNewButton_2 = new JButton("Sales");
		menuBar.add(btnNewButton_2);
		btnNewButton_2.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				SalesUI frame = new SalesUI();
				desktopPane.add(frame);
				frame.setVisible(true);
			}
		});

		
		JMenu mnOrders = new JMenu("Orders");
		menuBar.add(mnOrders);
		
		JMenuItem mntmManageSales = new JMenuItem("Manage sales");
		mntmManageSales.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				OrderUI frame = new OrderUI();
				desktopPane.add(frame);
				frame.setVisible(true);
			}
		});
		mnOrders.add(mntmManageSales);
		if(!(CurrentAccount.give_rank().equals("employee")))
		{
			JMenuItem mntmManageDiscounts = new JMenuItem("Manage discounts");
			mntmManageDiscounts.addActionListener(new ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{
					DiscountUI frame = new DiscountUI();
					desktopPane.add(frame);
					frame.setVisible(true);
				}
			});
			mnOrders.add(mntmManageDiscounts);
		}
		
		JMenu mnNewMenu = new JMenu("Leases");
		menuBar.add(mnNewMenu);
		
		JMenuItem mntmNewLease = new JMenuItem("New Lease");
		mntmNewLease.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				NewLeaseGUI frame = new NewLeaseGUI();
				desktopPane.add(frame);
				frame.setVisible(true);
			}
		});
		mnNewMenu.add(mntmNewLease);
		
		JMenuItem mntmManageLeases = new JMenuItem("Manage Leases");
		mnNewMenu.add(mntmManageLeases);
		mntmManageLeases.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				LeasesGUI frame = new LeasesGUI();
				desktopPane.add(frame);
				frame.setVisible(true);
			}
		});
		mnNewMenu.add(mntmManageLeases);
		
		JMenuItem mntmManageMachines = new JMenuItem("Manage Machines");
		mntmManageMachines.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				MachinesGUI frame = new MachinesGUI();
				desktopPane.add(frame);
				frame.setVisible(true);
			}
		});
		mnNewMenu.add(mntmManageMachines);
		
		JMenu mnAccounts = new JMenu("Accounts");
		menuBar.add(mnAccounts);
		
		JMenuItem mntmMyAccount = new JMenuItem("My account");
		mntmMyAccount.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				AccountDetails frame = new AccountDetails(CurrentAccount.get_id());
				desktopPane.add(frame);
				frame.setVisible(true);
			}
		});
		mnAccounts.add(mntmMyAccount);
		
		
		if(!(CurrentAccount.give_rank().equals("employee")))
		{
			JMenuItem mntmManageAccounts = new JMenuItem("Manage accounts");
			mntmManageAccounts.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent event) {
					AccountsUI frame = new AccountsUI();
					desktopPane.add(frame);
					frame.setVisible(true);
				}
			});
			mnAccounts.add(mntmManageAccounts);
		}

	}
}
